
package xj.graph2d.app.diagram;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*; 

import org.jdesktop.swingx.JXPanel;

import xj.graph2d.GraphDocument;
import xj.graph2d.ViewOptions;
import xj.graph2d.template.UMLTemplate;
import xj.graph2d.app.DrawApp;

import static xj.graph2d.layout.GraphDocumentUtil.*;

public class ClassDiagramViewOptions extends ViewOptions {

  public ClassDiagramViewOptions(GraphDocument document) { 
    super(document);
    setComponent(new ViewOptionsPanel()); 
  }

  class ViewOptionsPanel extends JXPanel { 
    
    ViewOptionsPanel() { 
      setOpaque(false);
      setLayout(new BorderLayout());

      JXPanel detailPanel = new JXPanel();
      detailPanel.setOpaque(false);
      detailPanel.setLayout(new BoxLayout(detailPanel, BoxLayout.Y_AXIS));

      add(detailPanel, BorderLayout.CENTER);

      OptionListener listener = new OptionListener();

      JLabel useLabel = new JLabel("Use-A relation: ");
      useShow = new JRadioButton("show");
      useHide = new JRadioButton("hide"); 
      useShow.setOpaque(false);
      useHide.setOpaque(false);
      useShow.addActionListener(listener);
      useHide.addActionListener(listener);
      ButtonGroup g1 = new ButtonGroup();
      g1.add(useShow);
      g1.add(useHide);
      useShow.setSelected(true); 
      detailPanel.add(useLabel);
      JXPanel panel = new JXPanel();
      panel.setOpaque(false);
      panel.setAlignmentX(Component.LEFT_ALIGNMENT);
      panel.setLayout(new BoxLayout(panel, BoxLayout.X_AXIS));
      panel.add(Box.createRigidArea(new Dimension(10,0)));
      panel.add(useShow);
      panel.add(useHide);
      useShow.setOpaque(false);
      useHide.setOpaque(false);
      detailPanel.add(panel);

      JLabel hasLabel = new JLabel("Has-A relation: ");
      hasShow = new JRadioButton("show");
      hasHide = new JRadioButton("hide"); 
      hasShow.setOpaque(false);
      hasHide.setOpaque(false);
      hasShow.addActionListener(listener);
      hasHide.addActionListener(listener);
      ButtonGroup g2 = new ButtonGroup();
      g2.add(hasShow);
      g2.add(hasHide);
      hasShow.setSelected(true); 
      detailPanel.add(hasLabel);
      panel = new JXPanel();
      panel.setOpaque(false);
      panel.setAlignmentX(Component.LEFT_ALIGNMENT);
      panel.setLayout(new BoxLayout(panel, BoxLayout.X_AXIS));
      panel.add(Box.createRigidArea(new Dimension(10,0)));
      panel.add(hasShow);
      panel.add(hasHide);
      detailPanel.add(panel);

      JLabel impLabel = new JLabel("Implements relation: ");
      impAll = new JRadioButton("all");
      impSingle = new JRadioButton("single");
      impNone = new JRadioButton("none");
      impAll.setOpaque(false);
      impSingle.setOpaque(false);
      impNone.setOpaque(false);
      impAll.addActionListener(listener);
      impSingle.addActionListener(listener);
      impNone.addActionListener(listener);
      ButtonGroup g3 = new ButtonGroup();
      g3.add(impAll);
      g3.add(impSingle);
      g3.add(impNone);
      impAll.setSelected(true);
      detailPanel.add(impLabel);
      panel = new JXPanel();
      panel.setOpaque(false);
      panel.setAlignmentX(Component.LEFT_ALIGNMENT);
      panel.setLayout(new BoxLayout(panel, BoxLayout.X_AXIS));
      panel.add(Box.createRigidArea(new Dimension(10,0)));
      panel.add(impAll);
      panel.add(impSingle);
      panel.add(impNone);
      detailPanel.add(panel);
 
      slider = new JSlider(SwingConstants.VERTICAL, 0, 100, 100); 
      slider.setOpaque(false);
      slider.setPreferredSize(new Dimension(20, 60));
      add(slider, BorderLayout.WEST);

      //showUseRelation = new JCheckBox("show use relation");          
      //add(showUseRelation);
      //showUseRelation.setSelected(true);
      //showUseRelation.addItemListener(new OptionListener());
            
    }
    
    JRadioButton useShow, useHide; 
    JRadioButton hasShow, hasHide; 
    JRadioButton impAll, impSingle, impNone; 

    JSlider slider; 

    class OptionListener implements ActionListener { 
      public void actionPerformed(ActionEvent e) { 
	if (document != null && view != null) { 
	  Object src = e.getSource(); 
	  if (src == useShow || src == useHide) {
	    setNodesVisibility(document, true);
	    boolean show = useShow.isSelected();
	    setEdgesVisibility(document, UMLTemplate.DEPENDENCY_EDGE, show);
	    hideUnconnectedExternalNodes(document);
	    setProperty("showUseRelation", show);
	  } else if (src == hasShow || src == hasHide) {
	    setNodesVisibility(document, true);
	    boolean show = hasShow.isSelected();
	    setEdgesVisibility(document, UMLTemplate.ASSOCIATION_EDGE, show);
	    hideUnconnectedExternalNodes(document);
	    setProperty("showUseRelation", show);
	  } else if (src == impAll || src == impSingle || src == impNone) { 
	    setNodesVisibility(document, true);
	    String imp; 
	    if (impAll.isSelected()) { 
	      imp = "all";
	      setEdgesVisibility(document, UMLTemplate.REALIZATION_EDGE, true);
	    } else if (impSingle.isSelected()) { 
	      imp = "single";
	      setSingleImplementationEdgesVisible(document);
	    } else {
	      imp = "none";
	      setEdgesVisibility(document, UMLTemplate.REALIZATION_EDGE, false);
	    }
	    hideUnconnectedExternalNodes(document);
	    setProperty("showImplementsRelation", imp);
	  }
	  document.doLayout();
	  DrawApp.fitCanvas(view);
	  view.repaint();	  
	}
      }
    }

    
    //JCheckBox showUseRelation; 
    
    /*
    class OptionListener implements ItemListener { 
      public void itemStateChanged(ItemEvent e) { 
	//System.out.println("ClassDiagramOptions.ViewOPtionsPanel.OptionListener " + showUseRelation.isSelected());

	setProperty("showUseRelation", showUseRelation.isSelected());
	if (document != null && view != null) { 
	  setEdgesVisibility(document, UMLTemplate.ASSOCIATION_EDGE,
			     showUseRelation.isSelected());
	  document.doLayout();
	  view.repaint();
	}
      }
    }
    */
  }

}